#include "get_arg.h"
#include <iostream>
#include <stdlib.h>
#include <sstream>
#include <ext/hash_map>
#include <fstream>
#include <seq_pairs.h>
#include <tab_delimited_str.h>
#include <portable_timer.h>
#include <print_error.h>
#include <boost/foreach.hpp>
#include <stlhelper.h>
#include <index_string.h>


using namespace std;
using namespace bcpp_matrix;
using boost::lexical_cast;
using std::ostringstream;
using std::istringstream;
using std::ofstream;
using std::ifstream;

typedef deque<vector<unsigned> >	deque_vec_u;
typedef __gnu_cxx::hash_map<unsigned, unsigned> t_hashuu;

struct t_clustered_id_pair : public std::pair<unsigned, unsigned>
{
	float dist;
	unsigned cluster_id;
	t_clustered_id_pair(unsigned id1_, unsigned id2_, float dist_)
		:std::pair<unsigned, unsigned>(id1_, id2_), dist(dist_), cluster_id(0)
	{
	}
};

typedef deque<t_clustered_id_pair> t_pairs;


//________________________________________________________________________________________

//		parse_blast_pairs_from_file

//________________________________________________________________________________________
struct do_parse_pairs
{
	t_index_string&	prot_id_str;
	t_pairs& 		prot_pairs;
	bool			hash_is_comment;
	do_parse_pairs(t_index_string&	prot_ids_, t_pairs& prot_pairs_, bool hash_is_comment_):
				prot_id_str(prot_ids_),  prot_pairs(prot_pairs_), 
				hash_is_comment(hash_is_comment_){	}
	void operator()(istream& is, unsigned)
	{
		// get data for one protein pair
		string prot_name1, prot_name2;

		// comment if false
		if (!seq_pairs::parse_ids(is, prot_name1, prot_name2, false, true))
			return;

			
		float dist = 5.0;
		is >> dist;
		if (!is)
			throw runtime_error("The pairs distances data is not in the correct format");

		// get protein and gene indices
		prot_pairs.push_back(t_clustered_id_pair(prot_id_str.index(prot_name1),  
												 prot_id_str.index(prot_name2), dist));
		std::cerr << prot_name1 << "\t" << prot_name2 << "\t" << dist << "\n";
	}
};


int main (int argc, char *argv[])
{
	t_program_args args;
	try
	{
		if (!process_command_line_options(argc, argv, args))
			return 1;
		t_index_string	prot_id_strs;
		t_pairs 		prot_pairs;

		do_parse_pairs parse_pairs(prot_id_strs,  prot_pairs, args.hash_is_comment);
		seq_pairs::read_data(parse_pairs, "test", args.VERBOSE());
		if (!prot_pairs.size())
			throw std::runtime_error("No pairs distances data supplied from STDIN.");
		args.VERBOSE(2) << "\t\t" << prot_pairs.size() << " pairs read.\n";

	}
	catch(std::exception& e)
	{
		std_print_error(args.CERR(),  e.what());
		return 1;
	}
 

	return 0;
}


